#import "RNCategory.h"
#import "RNItem.h"
#import "RNAisle.h"
#import "RNListItem.h"
#import "RNCatalog.h"
#import "RNConstants.h"

static BOOL showsChildren;
static BOOL useListItemsAsChildren;
static id currentFilterObject;

@implementation RNCategory 

@dynamic title;
@dynamic items;
@dynamic parentCategory;
@dynamic subcategories;
@dynamic aisles;
@dynamic listItems;

+(BOOL)showsChildren {
    return showsChildren;
}

+(void)setShowsChildren:(BOOL)shouldShowChildren {
    showsChildren = shouldShowChildren;
}

-(BOOL)isLeaf {
    return [[self subcategories] count] == 0;
}

+(NSSet *)keyPathsForValuesAffectingIsLeaf {
    return [NSSet setWithObject:@"subcategories"];
}

+(id)currentFilterObject {
    return currentFilterObject;
}

+(void)filterWithObject:(NSManagedObject *)filterObject {
    currentFilterObject = filterObject;
    useListItemsAsChildren = ![currentFilterObject isKindOfClass:[RNAisle class]];
}

+(NSSet *)keyPathsForValuesAffectingObjectDictionary {
    return [NSSet setWithObjects:@"items", @"subcategories", nil];
}

-(NSDictionary *)objectDictionary {
    NSArray *keys = [NSArray arrayWithObjects:@"title", @"itemsCount", nil];
    return [self dictionaryWithValuesForKeys:keys];
}

-(void)setObjectDictionary:(id)objectDictionary {
	NSLog(@"set category dictionary: %@", [objectDictionary class]);
    if ([objectDictionary isKindOfClass:[NSString class]])
        [self setTitle:objectDictionary];
    else if ([objectDictionary isKindOfClass:[NSDictionary class]])
        [self setTitle:[objectDictionary valueForKey:RNTitleKey]];
}

-(NSDictionary *)dictionaryRepresentation {
//    NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionary];
//    [dictionaryRepresentation setValue:[self title] forKey:@"title"];
//    [dictionaryRepresentation setValue:[[self parentCategory] URIRepresentation] forKey:@"parentCategory"];
//    NSMutableSet *subcategoryURIs = [NSMutableSet set];
//    for (NSManagedObject *subcategory in [self subcategories]) 
//        [subcategoryURIs addObject:[subcategory URIRepresentation]];
//    [dictionaryRepresentation setValue:subcategoryURIs forKey:@"subcategories"];
//    NSMutableSet *itemURIs = [NSMutableSet set];
//    for (NSManagedObject *item in [self items])
//        [itemURIs addObject:[item URIRepresentation]];
//    [dictionaryRepresentation setValue:itemURIs forKey:@"items"];
//    NSMutableSet *listItemURIs = [NSMutableSet set];
//    for (NSManagedObject *listItem in [self listItems])
//        [listItemURIs addObject:[listItem URIRepresentation]];
//    [dictionaryRepresentation setValue:listItemURIs forKey:@"listItems"];
//    return dictionaryRepresentation;
    return [NSDictionary dictionary];
}

-(NSNumber *)itemsCount {
    return [NSNumber numberWithInteger:[[self subcategories] count] + [[self items] count]];
}

+(NSSet *)keyPathsForValuesAffectingChildren {
    return [NSSet setWithObjects:@"listItems", @"items", nil];
    
}

-(NSSet *)children {
    if (!showsChildren)
        return nil;
    
    NSSet *filteredChildren = nil;
    if (useListItemsAsChildren) 
        filteredChildren = [[self listItems] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"list == %@", currentFilterObject]];
    else
        filteredChildren = [[self items] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"%@ IN aisles", currentFilterObject]];
    return filteredChildren;
}

-(id)parent {
    return [self parentCategory];
}

-(NSUInteger)displayIndex {
    return 0;
}

-(BOOL)canContainObjectsWithEntityNames:(NSArray *)entityNames {
    NSArray *entitiesContainableByMe = [NSArray arrayWithObjects:RNEntityNameItem, RNEntityNameListItem, RNEntityNameCategory, nil];
    NSMutableArray *remainderArray = [entityNames mutableCopy];
    [remainderArray removeObjectsInArray:entitiesContainableByMe];
    return [remainderArray count] == 0;
}

-(void)moveToContainer:(NSManagedObject *)container {
    if ([container isKindOfClass:[RNCategory class]])
        [self setParentCategory:(RNCategory *)container];
    if ([container isKindOfClass:[RNCatalog class]])
        [self setParentCategory:nil];
}

@end
